﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.BasicData
{
    public partial class Product : BasePage
    {
        protected int totalRecords = 0;
        protected int totalPages = 1;
        private IBrandBLL brandBLL = IocContainer.Instance.Resolve<IBrandBLL>();
        private static IProductBLL productBLL = IocContainer.Instance.Resolve<IProductBLL>();
        

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ControlAuthority(new List<WebControl> { btnAdd, btnUpdate, btnDelete, btnhidDelete, btnSearch, btnDisable, btnEnable }, 22);
                this.ddlBrand.DataSource = brandBLL.GetBrand("", null);
                this.ddlBrand.DataBind();
            }
           
        }

        [WebMethod]
        public static string getProductType(int brandId)
        {
            IProductTypeBLL productTypeBLL = IocContainer.Instance.Resolve<IProductTypeBLL>();
            DataTable productType = productTypeBLL.GetAllProductTypeByBrand(brandId);

            return SerializationHelper.DataTable2Json(productType);
        }

        [WebMethod]
        public static string GetProducts(string productName, string brandId, string productTypeId, string orderField, string orderValue, int pageIndex, int pagePerSize, string productModel, string isDisabled)
        {
            string strResult;
            IProductBLL productBLL = IocContainer.Instance.Resolve<IProductBLL>();
            StringBuilder strWhere = new StringBuilder(" where 1=1 ");
            if (!string.IsNullOrEmpty(productName))
            {
                strWhere.Append(" and ProductName like '%" + productName + "%'");
            }
            if (!string.IsNullOrEmpty(productName))
            {
                strWhere.Append(" and ProductModel like '%" + productModel + "%'");
            }
            if (brandId != "-1")
            {
                strWhere.Append(" and brandId=" + brandId);
            }
            if (productTypeId != "-1")
            {
                strWhere.Append(" and ProductTypeId=" + productTypeId);
            }
            if (productTypeId != "-1")
            {
                strWhere.Append(" and ProductTypeId=" + productTypeId);
            }
            if (isDisabled != "-1")
            {
                strWhere.Append(" and IsDisabled=" + isDisabled);
            }
            
            StringBuilder strOrderBy = new StringBuilder(" Order by ");
            strOrderBy.Append(!string.IsNullOrEmpty(orderField) ? " " + orderField : "  ProductName  ");
            strOrderBy.Append(!string.IsNullOrEmpty(orderValue) ? " " + orderValue : "  asc ");

            int totalRecords = 0;
            int totalPages = 0;
            DataTable dtProducts = productBLL.GetProductsByPage(strWhere.ToString(), strOrderBy.ToString(), pageIndex, pagePerSize, out totalRecords,
                                           out totalPages);

            strResult=SerializationHelper.DataTable2Json(dtProducts);
            
            return "{\"totalRecords\":\"" + totalRecords.ToString() + "\" ,\"totalPages\":\"" + totalPages.ToString() + "\",\"rows\":" +
                   strResult + " }";

        }

        [WebMethod]
        public static Boolean DeleteProducts(int productId)
        {
            Boolean result = false;
            try
            {
                productBLL.Delete(productId);
                result = true;
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
            }
            return result;
        }

        [WebMethod]
        public static Boolean DisableProducts(int productId)
        {
            Boolean result = false;
            try
            {
                productBLL.Disable(productId);
                result = true;
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
            }
            return result;
        }
        [WebMethod]
        public static Boolean EnableProducts(int productId)
        {
            Boolean result = false;
            try
            {
                productBLL.Enable(productId);
                result = true;
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
            }
            return result;
        }

        
        private string  Query()
        {
            int pageIndex = 0;
            int.TryParse(this.pageNum.Value, out pageIndex);
            int pagePerSize = 0;
            int.TryParse(this.numPerPageSelect.Value, out pagePerSize);
            StringBuilder strWhere = new StringBuilder(" where 1=1 ");
            if (!string.IsNullOrEmpty(this.txtProductName.Text))
            {
                strWhere.Append(" and ProductName like '%" + this.txtProductName.Text + "%'");
            }
            if (this.ddlBrand.SelectedValue != "-1")
            {
                strWhere.Append(" and brandId=" + this.ddlBrand.SelectedValue);
            }
            if (this.txtProductTypeId.Value != "-1")
            {
                strWhere.Append(" and ProductTypeId=" + this.txtProductTypeId.Value);
            }

            StringBuilder strOrderBy = new StringBuilder(" Order by ");
            strOrderBy.Append(!string.IsNullOrEmpty(this.orderField.Value) ? " " + this.orderField.Value : "  ProductName  ");
            strOrderBy.Append(!string.IsNullOrEmpty(this.orderValue.Value) ? " " + this.orderValue.Value : "  asc ");

            DataTable dtProducts = productBLL.GetProductsByPage(strWhere.ToString(), strOrderBy.ToString(), pageIndex, pagePerSize, out totalRecords,
                                           out totalPages);
            this.productsBody.Text = BuildProductsTable(dtProducts);
            return "";

        }

        private string BuildProductsTable(DataTable dtProducts)
        {
            StringBuilder result = new StringBuilder();
            if (dtProducts != null && dtProducts.Rows.Count > 0)
            {
                result.Append("<ul>");
                foreach (DataRow row in dtProducts.Rows)
                {
                    result.AppendFormat("<li rel='{5}'><img src='{0}' alt='产品图片' rel='../test.html' /><div class='block'><h4>{1}</h4><h3>产品编码：{2}</h3><h3>产品型号：{3}</h3><h3>产品尺寸：{4}</h3></div></li>",
                        row["Thumbnail1"], row["ProductName"], row["ProductCode"], row["ProductModel"], row["ProductSize"],row["ProductId"]);
                  
                   
                }
                result.Append("</ul>");
            }
            return result.ToString();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
           // Query();
        }

        

        protected void btnhidDelete_Click(object sender, EventArgs e)
        {

        }
    }
}
